﻿namespace Harness.Recording.LocateParsers
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using mshtml;

    internal class LinkLocateParseBehavior : LocateParser.ILocateParseBehavior
    {
        public string Parse(IHTMLElement element)
        {
            if (!(element is IHTMLAnchorElement))
            {
                element = element.parentElement;
            }

            if (!(element is IHTMLAnchorElement))
            {
                return string.Empty;
            }

            string s = element.innerText;

            if (!string.IsNullOrWhiteSpace(s))
            {
                s = s.Trim();
            }

            if (string.IsNullOrWhiteSpace(s))
            {
                s = element.title;
            }

            if (string.IsNullOrWhiteSpace(s))
            {
                s = ((IHTMLAnchorElement)element).href;
            }

            if (string.IsNullOrWhiteSpace(s))
            {
                return string.Empty;
            }

            return string.Format("link={0}", s);
        }
    }
}
